<?php
namespace View;

class PostView{
	
	const READRIGHTS = 0;
	const COMMENTRIGHTS = 1;
	const ADMINRIGHTS = 2;
	
	const WEBDEVTAG = "Webutveckling";
	const DESIGNTAG = "Design";
	const SCHOOLTAG = "Skola";
	const BUSINESSTAG = "Business";
	const LIFETAG = "Livet";
	
	private $m_tag = "tag";
	private $m_removeComment = "removeCommentId";
	private $m_removePost = "removePostId";
	private $m_postAction = "postaction";
	private $m_newposttitle = "postitle";
	private $m_newposttext = "posttext";
	private $m_newposttags = "tags";
	
	private $m_newfile = "file";
	
	public function generatePost($post){
		$html ='';
			$html .= "<div class='post'><h2>".$post->postTitle()."</h2><p>".$post->postText()."</p><img src='".$post->postPicture()."' />";
		
		return $html;
	}
	
	public function DoMakePostButtons(){
		$html ="<a id='makeNewPostButton' href='?$this->m_postAction=newpost'>Skriv nytt inlägg</a>";
		$html .="<a id='makeNewPictureButton' href='?$this->m_postAction=newpicture'>Ladda upp bild</a>";
		return $html;
	}
	
	public function generateCommentLine($post, $userrights){
		$html = "<div class='postInfoLine'><div class='postInfoItem'>".$post->postDate()."</div><div class='postInfoItem'><a class='showComments' href='#'>".$post->postNumberOfComments()." kommentarer</a></div>";
		if($userrights >= self::COMMENTRIGHTS ){
			$html .= "<div class='postInfoItem'><a class='commentLink' href='#'>Kommentera</a></div>";
			if($userrights == self::ADMINRIGHTS){
				$html .= "<div class='postInfoItem'><a class='removeLink' href='?$this->m_removePost=".$post->postId()."'>(admin)Ta bort inlägg.</a></div>";
			}
		}
		$html .= "</div>";
		return $html;
	}
	
	public function generatePostEnd(){
		return '</div>';
	}
	
	public function getPostViewTags(){
		if(isset($_GET[$this->m_tag])){
			return $_GET[$this->m_tag];
		}else{
			return false;
		}
	}
	
	public function generateComments($c, $userrights){
		$html = "<div class='postComments'><div class='commentByUser'>".$c->commentDate()." ".$c->commentUser()." säger:";
		if($userrights == self::ADMINRIGHTS){
			$html .= "<a style='float:right' href='?$this->m_removeComment=".$c->commentId()."'>(admin)Ta bort kommentar</a>";
		}
		$html .= "</div><div class='postComment'><p>".$c->commentText()."</p></div>";
		$html .= "</div>";
		return $html;
	}
	
	public function getPostId(){
		if(isset($_POST['postid'])){
			return $_POST['postid'];
		}
		
		return false;
	}
	
	public function getCommentText(){
		if(isset($_POST['commentField'])){
			return $_POST['commentField'];
		}
		
		return false;
	}
	
	public function TriedToComment(){
		if(isset($_POST['commentField']) && isset($_POST['postid'])){
			return true;
		}
		return false;
	}
	
	public function generateDoCommentForm($postid){
		$html = "<div class='commentForm'>
			<form method='post'>
				<textarea name='commentField'></textarea>
				<input type='hidden' name='postid' value='$postid' />
				<input type='submit' value='Skicka kommentar!' />
			</form>
		</div>";
		
		return $html;
	}
	
	public function TriedToRemoveComment(){
		if(isset($_GET[$this->m_removeComment])){
			return $_GET[$this->m_removeComment];
		}else{
			return false;
		}
	}
	
	public function TriedToRemovePost(){
		if(isset($_GET[$this->m_removePost])){
			return $_GET[$this->m_removePost];
		}else{
			return false;
		}
	}
	
	public function RemoveGetVar($varname) {
		$url = $_SERVER['PHP_SELF'];
	    list($urlpart, $qspart) = array_pad(explode('?', $url), 2, '');
	    parse_str($qspart, $qsvars);
	    unset($qsvars[$varname]);
	    $newqs = http_build_query($qsvars);
		header('location:'.$urlpart . '?' . $newqs);
	}
	
	public function GenerateNewPostBox(){
		$html = "<div class='post' id='newPostFormDiv'>
		<h2 class='orange'>Nytt inlägg</h2>
		<form method='post'>
		<table>
		<tr><td>
		<input name='$this->m_newposttitle' type='text' />
		</td></tr>
		<tr><td>
		<textarea name='$this->m_newposttext'></textarea>
		</td></tr>
		<tr><td>
		<input type='text' name='$this->m_newposttags' id='tagLine' class='longTagLine' value='' />
		</td></tr>
		<tr><td>
		<input type='button' value='Livet' class='tagbutton'/>
		<input type='button' value='Webutveckling'  class='tagbutton'/>
		<input type='button' value='Design'  class='tagbutton'/>
		<input type='button' value='Skola'  class='tagbutton'/>
		<input type='button' value='Business'  class='tagbutton'/>
		</td></tr>
		<tr><td>
		<input type='submit' value='Skapa inlägg' />
		</td></tr>
		</table>
		</form>
		</div>";
		return $html;
	}
	
	public function GenerateNewPictureBox(){
		$html = "<div class='post' id='newPictureFormDiv'>
		<h2 class='orange'>Nytt bildinlägg</h2>
		<form method='post' enctype='multipart/form-data'>
		<table>
		<tr><td>
		<input name='$this->m_newposttitle' type='text' />
		</td></tr>
		<tr><td>
		<input type='file' name='$this->m_newfile' />
		</td></tr>
		<tr><td>
		<textarea name='$this->m_newposttext'></textarea>
		</td></tr>
		<tr><td>
		<input type='text' name='$this->m_newposttags' id='tagLinepic' class='longTagLine' value='' />
		</td></tr>
		<tr><td>
		<input type='button' value='Livet' class='tagbuttonpic'/>
		<input type='button' value='Webutveckling'  class='tagbuttonpic'/>
		<input type='button' value='Design'  class='tagbuttonpic'/>
		<input type='button' value='Skola'  class='tagbuttonpic'/>
		<input type='button' value='Business'  class='tagbuttonpic'/>
		</td></tr>
		<tr><td>
		<input type='submit' value='Skapa inlägg' />
		</td></tr>
		</table>
		</form>
		</div>";
		return $html;
	}
	
	public function getNewPostText(){
		if(isset($_POST[$this->m_newposttext])){
			return $_POST[$this->m_newposttext];
		}
		return false;
	}
	public function getNewPostTitle(){
		if(isset($_POST[$this->m_newposttitle])){
			return $_POST[$this->m_newposttitle];
		}
		return false;
	}
	public function getPostTags(){
		if(isset($_POST[$this->m_newposttags])){
			$array = explode('; ', $_POST[$this->m_newposttags]);
			return $array;
		}else{
			return false;
		}
	}
	
	public function TriedToMakePost(){
		if(isset($_POST[$this->m_newposttext]) && isset($_POST[$this->m_newposttitle])){
			return true;
		}else{
			return false;
		}
	}
	
	public function TriedToUploadFile(){
		if(isset($_FILES[$this->m_newfile])){
			return true;
		}else{
			return false;
		}
	}
	
	public function getFile(){
		return $_FILES[$this->m_newfile];
	}
	
	public function addErrors($errors){ //lägger till och skriver ut felmeddelanden i en lista
		$html = '<ul class="errorList">';
		foreach($errors as $e){
			switch($e){
				case \PostHandler::FILENAME:
					$html .= "<li>Filnamnet är för långt.</li>";
					break;
				case \PostHandler::FILETYPE:
					$html .= "<li>Filen måste vara i formaten .jpg, .png eller .gif.</li>";
					break;
				case \PostHandler::TITLELENGTH:
					$html .= "<li>Titeln kräver 3-30 tecken.</li>";
					break;
				case \PostHandler::TEXTLENGTH:
					$html .= "<li>Texten i inlägget måste vara minst 10 tecken långt.</li>";
					break;
			}
		}
		$html .= "</ul>";
		return $html;
	}
}

?>